package com.cskaoyan.market.service.wx;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Author: chenjian
 * @Date: 2024/5/20 13:06
 */
@Service
public class WxStorageServiceImpl implements WxStorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;
    @Override
    public MarketStorage store(File imageFile) throws IOException {
        //将图片信息存入Storage中;
        String key=imageFile.getName();
        String name=imageFile.getName().substring(imageFile.getName().lastIndexOf("_")+1);
        String type="image/"+imageFile.getName().substring(imageFile.getName().lastIndexOf(".")+1);
        long size=imageFile.length();
        String url="wx/storage/fetch/"+key;
        LocalDateTime addTime = LocalDateTime.now();
        LocalDateTime updateTime = LocalDateTime.now();
        Boolean deleted = false;
        try{
            MarketStorage marketStorage = new MarketStorage();
            marketStorage.setId(null);
            marketStorage.setKey(key);
            marketStorage.setName(name);
            marketStorage.setType(type);
            marketStorage.setSize((int) size);
            marketStorage.setUrl(url);
            marketStorage.setAddTime(addTime);
            marketStorage.setUpdateTime(updateTime);
            marketStorage.setDeleted(deleted);
            marketStorageMapper.insertSelective(marketStorage);
            return marketStorage;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
